package com.huawei.bookborrowms.book.service.impl;

import com.huawei.base.BaseRes;
import com.huawei.bookborrowms.book.dao.BookDao;
import com.huawei.bookborrowms.book.dto.BookDto;
import com.huawei.bookborrowms.book.dto.PageDto;
import com.huawei.bookborrowms.book.entity.BookEntity;
import com.huawei.bookborrowms.book.service.BookService;
import com.huawei.utils.BookTypeUtil;
import com.huawei.utils.IsBorrowUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 黄后林
 * @date 2022-11-10 17:25:06
 * @note 程序员是用代码喂出来的
 */
@Service
public class BookServiceImpl implements BookService {

    @Autowired
    private BookDao dao;
    @Override
    public BaseRes findAllBooks(BookDto bookDto) {

        //PageDto page = bookDto.getPageDto();
        Integer before= (bookDto.getPageNumber()-1)*bookDto.getPageSize();
        Integer after=bookDto.getPageSize();

        Map<String,Object> map = new HashMap<>();
        List<BookEntity> books = dao.findAllBooks(bookDto.getBookType(),bookDto.getBookName(), bookDto.getIsBorrow(),before,after );
        for (BookEntity book : books) {
            book.setBookTypeName(BookTypeUtil.getBookTypeName(book.getBookType()));
            book.setIsBorrowName(IsBorrowUtil.getIsBorrowName(book.getIsBorrow()));
        }

        map.put("books",books);

        Integer counter = dao.counterBook(bookDto.getBookType(), bookDto.getBookName(), bookDto.getIsBorrow());
        map.put("counter",counter/3+1);
        return BaseRes.getSuccess(map);
    }
}
